package br.ufrj.dcc.xml.view;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

import br.ufrj.dcc.xml.ActionConstants;
import br.ufrj.dcc.xml.dynamicintervals.vo.Document;

import com.birosoft.liquid.LiquidLookAndFeel;

public class DynamicForm extends JFrame implements ActionConstants {

	private static final long serialVersionUID = 1L;

	private ActionListener listener; // @jve:decl-index=0:

	private JPanel jContentPane = null;

	private JMenuBar jJMenuBar = null;

	private JMenu jMenuRepositorio = null;

	private JMenu jMenuXML = null;

	private JMenu jMenuSair = null;

	private JMenuItem jMenuItemConectar = null;

	private JMenuItem jMenuItemEsquema = null;

	private JMenuItem jMenuItemExcluir = null;

	private JMenuItem jMenuItemArmDocXML = null;

	private JMenuItem jMenuItemListXML = null;

	private JMenuItem jMenuItemRecXML = null;

	private JMenuItem jMenuItemExcluirXML = null;

	private JMenuItem popupMenuExcluirXml = null;

	private JMenuItem popupMenuRecXml = null;

	private JMenuItem popupMenuLimparXml = null;
	
	private JMenuItem popupMenuSalvarXml = null;

	private JMenuItem popupMenuLimparSql = null;

	private JPanel jPanelXML = null;

	private JMenu jMenuAbout = null;

	private JPanel jPanelQuery = null;

	private JScrollPane jScrollPaneXml = null;

	private JScrollPane jScrollPaneQuery = null;

	private JPanel jPanelDocs = null;

	private JScrollPane jScrollPaneDocs = null;

	private JList jListDocs = null;

	private DefaultListModel listModel = new DefaultListModel();

	private JTextArea jTextPaneXml = null;

	private JTextArea jTextPaneQuery = null;

	private int listSelectedIndex = -1;

	private JMenuItem jMenuItemBuscar = null;

	public int getListSelectedIndex() {
		if (listSelectedIndex == -1) {
			listSelectedIndex = jListDocs.getSelectedIndex();
		}
		return listSelectedIndex;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenuRepositorio());
			jJMenuBar.add(getJMenuXML());
			jJMenuBar.add(getJMenuAbout());
			jJMenuBar.add(getJMenuSair());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenuRepositorio
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuRepositorio() {
		if (jMenuRepositorio == null) {
			jMenuRepositorio = new JMenu();
			jMenuRepositorio.setText("Repositório");
			jMenuRepositorio.add(getJMenuItemConectar());
			jMenuRepositorio.add(getJMenuItemEsquema());
			jMenuRepositorio.add(getJMenuItemExcluir());
		}
		return jMenuRepositorio;
	}

	/**
	 * This method initializes jMenuXML
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuXML() {
		if (jMenuXML == null) {
			jMenuXML = new JMenu();
			jMenuXML.setText("XML");
			jMenuXML.add(getJMenuItemArmDocXML());
			jMenuXML.add(getJMenuItemListXML());
			jMenuXML.add(getJMenuItemRecXML());
			jMenuXML.add(getJMenuItemBuscar());			
			jMenuXML.add(getJMenuItemExcluirXML());
		}
		return jMenuXML;
	}

	/**
	 * This method initializes jMenuSair
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuSair() {
		if (jMenuSair == null) {
			jMenuSair = new JMenu();
			jMenuSair.setText("Sair");
			jMenuSair.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					listener.actionPerformed(new ActionEvent(jMenuSair, 0,
							Integer.toString(SAIR)));
				}
			});
		}
		return jMenuSair;
	}

	/**
	 * This method initializes jMenuItemConectar
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemConectar() {
		if (jMenuItemConectar == null) {
			jMenuItemConectar = new JMenuItem();
			jMenuItemConectar.setText("Conectar");
			jMenuItemConectar.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/connect.png")));
		}
		return jMenuItemConectar;
	}

	/**
	 * This method initializes jMenuItemEsquema
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemEsquema() {
		if (jMenuItemEsquema == null) {
			jMenuItemEsquema = new JMenuItem();
			jMenuItemEsquema.setText("Criar Esquema");
			jMenuItemEsquema.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/package-up.png")));
		}
		return jMenuItemEsquema;
	}

	/**
	 * This method initializes jMenuItemExcluir
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemExcluir() {
		if (jMenuItemExcluir == null) {
			jMenuItemExcluir = new JMenuItem();
			jMenuItemExcluir.setText("Excluir");
			jMenuItemExcluir.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/Package-remove.png")));
		}
		return jMenuItemExcluir;
	}

	/**
	 * This method initializes jMenuItemArmDocXML
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemArmDocXML() {
		if (jMenuItemArmDocXML == null) {
			jMenuItemArmDocXML = new JMenuItem();
			jMenuItemArmDocXML.setText("Armazenar Doc. XML");
			jMenuItemArmDocXML.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/add-database_32x32.png")));
		}
		return jMenuItemArmDocXML;
	}

	/**
	 * This method initializes jMenuItemListXML
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemListXML() {
		if (jMenuItemListXML == null) {
			jMenuItemListXML = new JMenuItem();
			jMenuItemListXML.setText("Lista Doc. XML");
			jMenuItemListXML.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/db_list.png")));
		}
		return jMenuItemListXML;
	}

	/**
	 * This method initializes jMenuItemRecXML
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemRecXML() {
		if (jMenuItemRecXML == null) {
			jMenuItemRecXML = new JMenuItem();
			jMenuItemRecXML.setText("Recuperar Doc. XML");
			jMenuItemRecXML.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/find_base.png")));
		}
		return jMenuItemRecXML;
	}

	/**
	 * This method initializes jMenuItemExcluirXML
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getJMenuItemExcluirXML() {
		if (jMenuItemExcluirXML == null) {
			jMenuItemExcluirXML = new JMenuItem();
			jMenuItemExcluirXML.setText("Excluir Doc. XML");
			jMenuItemExcluirXML.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/delete-database_32x32.png")));
		}
		return jMenuItemExcluirXML;
	}

	/**
	 * This method initializes jPanelConsole
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelConsole() {
		if (jPanelXML == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.insets = new Insets(0, 7, 4, 8);
			jPanelXML = new JPanel();
			jPanelXML.setLayout(new GridBagLayout());
			jPanelXML.setPreferredSize(new Dimension(490, 270));
			jPanelXML.setBorder(BorderFactory.createTitledBorder(null,
					"Xml View", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Verdana",
							Font.BOLD, 12), new Color(51, 51, 51)));
			jPanelXML.add(getJScrollPaneXml(), gridBagConstraints2);
		}
		return jPanelXML;
	}

	/**
	 * This method initializes jMenuAbout
	 * 
	 * @return javax.swing.JMenu
	 */
	private JMenu getJMenuAbout() {
		if (jMenuAbout == null) {
			jMenuAbout = new JMenu();
			jMenuAbout.setText("Sobre");
			jMenuAbout.setActionCommand(Integer.toString(SOBRE));
			jMenuAbout.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					listener.actionPerformed(new ActionEvent(jMenuAbout, 0,
							Integer.toString(SOBRE)));
				}
			});
		}
		return jMenuAbout;
	}

	/**
	 * This method initializes jPanelQuery
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelQuery() {
		if (jPanelQuery == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.weighty = 1.0;
			gridBagConstraints3.insets = new Insets(0, 7, 4, 8);
			jPanelQuery = new JPanel();
			jPanelQuery.setBorder(BorderFactory.createTitledBorder(null,
					"Query View", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Verdana",
							Font.BOLD, 12), new Color(51, 51, 51)));
			jPanelQuery.setLayout(new GridBagLayout());
			jPanelQuery
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			jPanelQuery.add(getJScrollPaneQuery(), gridBagConstraints3);
		}
		return jPanelQuery;
	}

	/**
	 * This method initializes jScrollPaneXml
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPaneXml() {
		if (jScrollPaneXml == null) {
			jScrollPaneXml = new JScrollPane(getJTextPaneXml());
			jScrollPaneXml.setPreferredSize(new Dimension(475, 240));
			jScrollPaneXml
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
			jScrollPaneXml.setBackground(Color.white);
			jScrollPaneXml.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jScrollPaneXml.setViewportView(getJTextPaneXml());
			jScrollPaneXml.setBorder(BorderFactory.createLineBorder(SystemColor.textInactiveText, 2));
		}
		return jScrollPaneXml;
	}

	/**
	 * This method initializes jScrollPaneQuery
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPaneQuery() {
		if (jScrollPaneQuery == null) {
			jScrollPaneQuery = new JScrollPane(getJTextPaneQuery());
			jScrollPaneQuery.setPreferredSize(new Dimension(475, 150));
			jScrollPaneQuery.setViewportView(getJTextPaneQuery());
			jScrollPaneQuery.setBorder(BorderFactory.createLineBorder(
					SystemColor.textInactiveText, 2));
		}
		return jScrollPaneQuery;
	}

	/**
	 * This method initializes jPanelDocs
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelDocs() {
		if (jPanelDocs == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.ipadx = 120;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.weighty = 1.0;
			gridBagConstraints4.insets = new Insets(0, 0, 124, 0);
			jPanelDocs = new JPanel();
			jPanelDocs.setLayout(new GridBagLayout());
			jPanelDocs.setPreferredSize(new Dimension(0, 450));
			jPanelDocs.setBorder(BorderFactory.createTitledBorder(null,
					"Docs Armazenados", TitledBorder.LEFT,
					TitledBorder.DEFAULT_POSITION, new Font("Verdana",
							Font.BOLD, 12), new Color(51, 51, 51)));
			jPanelDocs.add(getJScrollPaneDocs(), gridBagConstraints4);
		}
		return jPanelDocs;
	}

	/**
	 * This method initializes jScrollPaneDocs
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPaneDocs() {
		if (jScrollPaneDocs == null) {
			jScrollPaneDocs = new JScrollPane(getJListDocs());
			jScrollPaneDocs.setPreferredSize(new Dimension(50, 300));
			jScrollPaneDocs.setViewportView(getJListDocs());
			jScrollPaneDocs.setBorder(BorderFactory.createLineBorder(
					SystemColor.textInactiveText, 2));
		}
		return jScrollPaneDocs;
	}

	/**
	 * This method initializes jListDocs
	 * 
	 * @return javax.swing.JList
	 */
	public JList getJListDocs() {
		if (jListDocs == null) {
			jListDocs = new JList();
			jListDocs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jListDocs
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			jListDocs.setToolTipText("Ids dos Docs XML");
		}
		return jListDocs;
	}

	/**
	 * This method initializes jTextPaneXml
	 * 
	 * @return javax.swing.JTextPane
	 */
	public JTextArea getJTextPaneXml() {
		if (jTextPaneXml == null) {
			jTextPaneXml = new JTextArea() {
				private static final long serialVersionUID = 1L;

				// Don't Wrap.
				public void setSize(Dimension d)
				{
					if (d.width < getParent().getSize().width)
						d.width = getParent().getSize().width;
	 
					super.setSize(d);
				}
	 
				public boolean getScrollableTracksViewportWidth()
				{
					return false;
				}				
			};			
			jTextPaneXml.setBackground(SystemColor.controlLtHighlight);
			jTextPaneXml.setEditable(false);
			jTextPaneXml.setFont(new Font("Courier New", Font.BOLD, 12));

			final JPopupMenu popup = new JPopupMenu("XML View");

			popupMenuLimparXml = new JMenuItem("Limpar Xml");
			popupMenuLimparXml.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/remove file.png")));

			popup.add(popupMenuLimparXml);

			popupMenuSalvarXml = new JMenuItem("Salvar Xml");
			popupMenuSalvarXml.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/add-database_32x32.png")));

			popup.add(popupMenuSalvarXml);
			
			
			jTextPaneXml.add(popup);

			jTextPaneXml.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					showPopup(e);
				}

				public void mouseReleased(MouseEvent e) {
					showPopup(e);
				}

				private void showPopup(MouseEvent e) {
					if (e.isPopupTrigger()) {
						popup.show(e.getComponent(), e.getX(), e.getY());
					}
				}

				public void mouseClicked(MouseEvent e) {
					showPopup(e);
				}
			});
		}
		return jTextPaneXml;
	}

	/**
	 * This method initializes jTextPaneQuery
	 * 
	 * @return javax.swing.JTextPane
	 */
	public JTextArea getJTextPaneQuery() {
		if (jTextPaneQuery == null) {
			jTextPaneQuery = new JTextArea();
			jTextPaneQuery.setBackground(SystemColor.controlLtHighlight);
			jTextPaneQuery.setEditable(false);
			jTextPaneQuery.setFont(new Font("Courier New", Font.BOLD, 12));

			final JPopupMenu popup = new JPopupMenu("SQL View");

			popupMenuLimparSql = new JMenuItem("Limpar Queries");
			popupMenuLimparSql.setIcon(new ImageIcon(getClass().getResource(
					"/imgs/remove file.png")));

			popup.add(popupMenuLimparSql);

			jTextPaneQuery.add(popup);

			jTextPaneQuery.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					showPopup(e);
				}

				public void mouseReleased(MouseEvent e) {
					showPopup(e);
				}

				private void showPopup(MouseEvent e) {
					if (e.isPopupTrigger()) {
						popup.show(e.getComponent(), e.getX(), e.getY());
					}
				}

				public void mouseClicked(MouseEvent e) {
					showPopup(e);
				}
			});

		}
		return jTextPaneQuery;
	}

	public void appendText(String text, JTextPane textPane) {
		String newText = textPane.getText() + text;
		textPane.setText(newText);
	}

	public static void changeLookAndFeel(String look) {
		try {
			UIManager.setLookAndFeel(look);
			LiquidLookAndFeel.setLiquidDecorations(true);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * This is the default constructor
	 * 
	 * @param look
	 */
	public DynamicForm() {
		super();
		initialize();

		// Centraliza..
		Dimension us = this.getSize(), them = Toolkit.getDefaultToolkit()
				.getScreenSize();
		int newX = (them.width - us.width) / 2;
		int newY = (them.height - us.height) / 2;
		this.setLocation(newX, newY);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(705, 520);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setName("DynamicForm");
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		mountPopupMenu();

		// coloca a view no seu estado inicial..
		setButtonState(NAO_CONECTADO);

		this.setTitle("Dynamic Interval");
	}

	private void mountPopupMenu() {

		final JPopupMenu popup = new JPopupMenu("Lista de Documentos XML");

		popupMenuRecXml = new JMenuItem("Recupera Documento");
		popupMenuRecXml.setIcon(new ImageIcon(getClass().getResource(
				"/imgs/find_base.png")));

		popupMenuExcluirXml = new JMenuItem("Excluir Documento");
		popupMenuExcluirXml.setIcon(new ImageIcon(getClass().getResource(
				"/imgs/delete-database_32x32.png")));

		popup.add(popupMenuRecXml);
		popup.add(popupMenuExcluirXml);

		jListDocs.add(popup);

		jListDocs.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				// Detecta o Click Direito
				if (SwingUtilities.isRightMouseButton(e)) {
					listSelectedIndex = jListDocs.locationToIndex(e.getPoint());
					jListDocs.setSelectionInterval(listSelectedIndex,
							listSelectedIndex);
				}
				showPopup(e);
			}

			public void mouseReleased(MouseEvent e) {
				// Detecta o Click Direito
				if (SwingUtilities.isRightMouseButton(e)) {
					listSelectedIndex = jListDocs.locationToIndex(e.getPoint());
					jListDocs.setSelectionInterval(listSelectedIndex,
							listSelectedIndex);
				}
				showPopup(e);
			}

			private void showPopup(MouseEvent e) {
				if (e.isPopupTrigger()) {
					popup.show(e.getComponent(), e.getX(), e.getY());
				}
			}

			public void mouseClicked(MouseEvent e) {
				setButtonState(ITEM_SELECIONADO);
				// Detecta o Click Direito
				if (SwingUtilities.isRightMouseButton(e)) {
					listSelectedIndex = jListDocs.locationToIndex(e.getPoint());
					jListDocs.setSelectionInterval(listSelectedIndex,
							listSelectedIndex);
				}
				showPopup(e);
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = -1;
			gridBagConstraints31.ipadx = 120;
			gridBagConstraints31.fill = GridBagConstraints.BOTH;
			gridBagConstraints31.weightx = 0.3;
			gridBagConstraints31.weighty = 1.0;
			gridBagConstraints31.gridheight = 2;
			gridBagConstraints31.gridy = -1;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.ipadx = 25;
			gridBagConstraints21.weightx = 0.7;
			gridBagConstraints21.weighty = 0.2;
			gridBagConstraints21.fill = GridBagConstraints.BOTH;
			gridBagConstraints21.gridy = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = -1;
			gridBagConstraints11.ipadx = 10;
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.weightx = 0.7;
			gridBagConstraints11.weighty = 0.8;
			gridBagConstraints11.gridy = -1;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getJPanelConsole(), gridBagConstraints11);
			jContentPane.add(getJPanelQuery(), gridBagConstraints21);
			jContentPane.add(getJPanelDocs(), gridBagConstraints31);
		}
		return jContentPane;
	}

	/**
	 * Associa o listenar desta classe, e seta todos os actionCommand's dos
	 * itens do menu deste form..
	 * 
	 * @param listener
	 */
	public void setListener(ActionListener listener) {
		this.listener = listener;

		// Menu Repositório
		jMenuItemConectar.addActionListener(listener);
		jMenuItemConectar.setActionCommand(Integer.toString(CONECTAR));

		jMenuItemEsquema.addActionListener(listener);
		jMenuItemEsquema.setActionCommand(Integer.toString(CRIAR_ESQUEMA));

		jMenuItemExcluir.addActionListener(listener);
		jMenuItemExcluir.setActionCommand(Integer.toString(EXCLUIR_ESQUEMA));

		// Menu XML
		jMenuItemArmDocXML.addActionListener(listener);
		jMenuItemArmDocXML.setActionCommand(Integer.toString(ARMAZENAR_XML));

		jMenuItemListXML.addActionListener(listener);
		jMenuItemListXML.setActionCommand(Integer.toString(LISTAR_XML));

		jMenuItemBuscar.addActionListener(listener);
		jMenuItemBuscar.setActionCommand(Integer.toString(MOSTRAR_TELA_BUSCAS));
		
		popupMenuRecXml.addActionListener(listener);
		popupMenuRecXml.setActionCommand(Integer.toString(RECUPERAR_XML));

		popupMenuExcluirXml.addActionListener(listener);
		popupMenuExcluirXml.setActionCommand(Integer.toString(EXCLUIR_XML));

		popupMenuLimparXml.addActionListener(listener);
		popupMenuLimparXml.setActionCommand(Integer.toString(LIMPAR_XML));

		popupMenuSalvarXml.addActionListener(listener);
		popupMenuSalvarXml.setActionCommand(Integer.toString(SALVAR_XML));
		
		popupMenuLimparSql.addActionListener(listener);
		popupMenuLimparSql.setActionCommand(Integer.toString(LIMPAR_SQL));

		jMenuItemRecXML.addActionListener(listener);
		jMenuItemRecXML.setActionCommand(Integer.toString(RECUPERAR_XML));

		jMenuItemExcluirXML.addActionListener(listener);
		jMenuItemExcluirXML.setActionCommand(Integer.toString(EXCLUIR_XML));

		// Sair
		jMenuSair.addActionListener(listener);
		jMenuSair.setActionCommand(Integer.toString(SAIR));

		// Sobre
		jMenuAbout.addActionListener(listener);
		jMenuAbout.setActionCommand(Integer.toString(SOBRE));
	}

	public void setDocList(List docs) {
		if (docs != null) {
			Iterator it = docs.iterator();
			listModel.clear();
			while (it.hasNext()) {
				Document doc = (Document) it.next();
				listModel.addElement(doc.getName());
			}
			jListDocs.setModel(listModel);
		}
	}

	public void setDocElemet(Document doc) {
		listModel.addElement(doc.getName());
		jListDocs.setModel(listModel);
	}

	public void deleteItem(int index) {
		listModel.removeElementAt(index);
	}

	public void deleteAllItems() {
		listModel.removeAllElements();
	}

	/**
	 * M�quina de estados para a view..
	 * 
	 * @param state
	 */
	public void setButtonState(int state) {
		switch (state) {
		case CONECTADO:
			jMenuItemConectar.setEnabled(false);
			jMenuItemEsquema.setEnabled(true);
			jMenuItemExcluir.setEnabled(true);
			jMenuItemListXML.setEnabled(true);
			jMenuItemArmDocXML.setEnabled(true);
			jMenuItemBuscar.setEnabled(true);

			jMenuItemRecXML.setEnabled(false);
			jMenuItemExcluirXML.setEnabled(false);
			popupMenuExcluirXml.setEnabled(false);
			popupMenuRecXml.setEnabled(false);
			break;

		case NAO_CONECTADO:
			jMenuItemConectar.setEnabled(true);
			jMenuItemEsquema.setEnabled(false);
			jMenuItemExcluir.setEnabled(false);
			jMenuItemListXML.setEnabled(false);
			jMenuItemArmDocXML.setEnabled(false);
			jMenuItemBuscar.setEnabled(false);

			jMenuItemRecXML.setEnabled(false);
			jMenuItemExcluirXML.setEnabled(false);
			popupMenuExcluirXml.setEnabled(false);
			popupMenuRecXml.setEnabled(false);
			break;

		case ESQUEMA_EXCLUIDO:
			jMenuItemListXML.setEnabled(false);
			jMenuItemArmDocXML.setEnabled(false);
			jMenuItemRecXML.setEnabled(false);
			jMenuItemBuscar.setEnabled(false);
			jMenuItemExcluirXML.setEnabled(false);
			popupMenuExcluirXml.setEnabled(false);
			popupMenuRecXml.setEnabled(false);
			break;

		case ESQUEMA_CRIADO:
			jMenuItemListXML.setEnabled(true);
			jMenuItemArmDocXML.setEnabled(true);
			jMenuItemRecXML.setEnabled(false);
			jMenuItemBuscar.setEnabled(true);
			jMenuItemExcluirXML.setEnabled(false);
			popupMenuExcluirXml.setEnabled(false);
			popupMenuRecXml.setEnabled(false);
			break;

		case ITEM_SELECIONADO:
			jMenuItemRecXML.setEnabled(true);
			jMenuItemExcluirXML.setEnabled(true);
			popupMenuExcluirXml.setEnabled(true);
			popupMenuRecXml.setEnabled(true);
			break;

		case ITEM_NAO_SELECIONADO:
			jMenuItemRecXML.setEnabled(false);
			jMenuItemExcluirXML.setEnabled(false);
			popupMenuExcluirXml.setEnabled(false);
			popupMenuRecXml.setEnabled(false);
			break;

		}
	}

	/**
	 * This method initializes jMenuItemBuscar	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemBuscar() {
		if (jMenuItemBuscar == null) {
			jMenuItemBuscar = new JMenuItem();
			jMenuItemBuscar.setText("Buscar");
			jMenuItemBuscar.setIcon(new ImageIcon(getClass().getResource(
			"/imgs/find_base.png")));			
		}
		return jMenuItemBuscar;
	}
} // @jve:decl-index=0:visual-constraint="10,10"
